// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyExpression.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>

// ****************************************************************************
// Module: PyExpression
//
// Purpose:
//   This class contains an expression.
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a Expression.
//
struct ExpressionObject
{
    PyObject_HEAD
    Expression *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewExpression(int);
std::string
PyExpression_ToString(const Expression *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    snprintf(tmpStr, 1000, "%sname = \"%s\"\n", prefix, atts->GetName().c_str());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%sdefinition = \"%s\"\n", prefix, atts->GetDefinition().c_str());
    str += tmpStr;
    if(atts->GetHidden())
        snprintf(tmpStr, 1000, "%shidden = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%shidden = 0\n", prefix);
    str += tmpStr;
    const char *type_names = "Unknown, ScalarMeshVar, VectorMeshVar, TensorMeshVar, SymmetricTensorMeshVar, "
        "ArrayMeshVar, CurveMeshVar, Mesh, Material, "
        "Species";
    switch (atts->GetType())
    {
      case Expression::Unknown:
          snprintf(tmpStr, 1000, "%stype = %sUnknown  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      case Expression::ScalarMeshVar:
          snprintf(tmpStr, 1000, "%stype = %sScalarMeshVar  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      case Expression::VectorMeshVar:
          snprintf(tmpStr, 1000, "%stype = %sVectorMeshVar  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      case Expression::TensorMeshVar:
          snprintf(tmpStr, 1000, "%stype = %sTensorMeshVar  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      case Expression::SymmetricTensorMeshVar:
          snprintf(tmpStr, 1000, "%stype = %sSymmetricTensorMeshVar  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      case Expression::ArrayMeshVar:
          snprintf(tmpStr, 1000, "%stype = %sArrayMeshVar  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      case Expression::CurveMeshVar:
          snprintf(tmpStr, 1000, "%stype = %sCurveMeshVar  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      case Expression::Mesh:
          snprintf(tmpStr, 1000, "%stype = %sMesh  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      case Expression::Material:
          snprintf(tmpStr, 1000, "%stype = %sMaterial  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      case Expression::Species:
          snprintf(tmpStr, 1000, "%stype = %sSpecies  # %s\n", prefix, prefix, type_names);
          str += tmpStr;
          break;
      default:
          break;
    }

    if(atts->GetFromDB())
        snprintf(tmpStr, 1000, "%sfromDB = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%sfromDB = 0\n", prefix);
    str += tmpStr;
    if(atts->GetFromOperator())
        snprintf(tmpStr, 1000, "%sfromOperator = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%sfromOperator = 0\n", prefix);
    str += tmpStr;
    snprintf(tmpStr, 1000, "%soperatorName = \"%s\"\n", prefix, atts->GetOperatorName().c_str());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%smeshName = \"%s\"\n", prefix, atts->GetMeshName().c_str());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%sdbName = \"%s\"\n", prefix, atts->GetDbName().c_str());
    str += tmpStr;
    if(atts->GetAutoExpression())
        snprintf(tmpStr, 1000, "%sautoExpression = 1\n", prefix);
    else
        snprintf(tmpStr, 1000, "%sautoExpression = 0\n", prefix);
    str += tmpStr;
    return str;
}

static PyObject *
Expression_Notify(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_SetName(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the name in the object.
    obj->data->SetName(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetName(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetName().c_str());
    return retval;
}

/*static*/ PyObject *
Expression_SetDefinition(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the definition in the object.
    obj->data->SetDefinition(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetDefinition(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetDefinition().c_str());
    return retval;
}

/*static*/ PyObject *
Expression_SetHidden(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the hidden in the object.
    obj->data->SetHidden(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetHidden(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetHidden()?1L:0L);
    return retval;
}

/*static*/ PyObject *
Expression_SetType(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    int cval = int(val);

    if ((val == -1 && PyErr_Occurred()) || long(cval) != val)
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ int");
    }

    if (cval < 0 || cval >= 10)
    {
        std::stringstream ss;
        ss << "An invalid type value was given." << std::endl;
        ss << "Valid values are in the range [0,9]." << std::endl;
        ss << "You can also use the following symbolic names:";
        ss << " Unknown";
        ss << ", ScalarMeshVar";
        ss << ", VectorMeshVar";
        ss << ", TensorMeshVar";
        ss << ", SymmetricTensorMeshVar";
        ss << ", ArrayMeshVar";
        ss << ", CurveMeshVar";
        ss << ", Mesh";
        ss << ", Material";
        ss << ", Species";
        return PyErr_Format(PyExc_ValueError, ss.str().c_str());
    }

    Py_XDECREF(packaged_args);

    // Set the type in the object.
    obj->data->SetType(Expression::ExprType(cval));

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetType(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetType()));
    return retval;
}

/*static*/ PyObject *
Expression_SetFromDB(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the fromDB in the object.
    obj->data->SetFromDB(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetFromDB(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetFromDB()?1L:0L);
    return retval;
}

/*static*/ PyObject *
Expression_SetFromOperator(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the fromOperator in the object.
    obj->data->SetFromOperator(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetFromOperator(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetFromOperator()?1L:0L);
    return retval;
}

/*static*/ PyObject *
Expression_SetOperatorName(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the operatorName in the object.
    obj->data->SetOperatorName(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetOperatorName(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetOperatorName().c_str());
    return retval;
}

/*static*/ PyObject *
Expression_SetMeshName(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the meshName in the object.
    obj->data->SetMeshName(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetMeshName(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetMeshName().c_str());
    return retval;
}

/*static*/ PyObject *
Expression_SetDbName(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the dbName in the object.
    obj->data->SetDbName(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetDbName(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetDbName().c_str());
    return retval;
}

/*static*/ PyObject *
Expression_SetAutoExpression(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    bool cval = bool(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ bool");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ bool");
    }

    Py_XDECREF(packaged_args);

    // Set the autoExpression in the object.
    obj->data->SetAutoExpression(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
Expression_GetAutoExpression(PyObject *self, PyObject *args)
{
    ExpressionObject *obj = (ExpressionObject *)self;
    PyObject *retval = PyInt_FromLong(obj->data->GetAutoExpression()?1L:0L);
    return retval;
}



PyMethodDef PyExpression_methods[EXPRESSION_NMETH] = {
    {"Notify", Expression_Notify, METH_VARARGS},
    {"SetName", Expression_SetName, METH_VARARGS},
    {"GetName", Expression_GetName, METH_VARARGS},
    {"SetDefinition", Expression_SetDefinition, METH_VARARGS},
    {"GetDefinition", Expression_GetDefinition, METH_VARARGS},
    {"SetHidden", Expression_SetHidden, METH_VARARGS},
    {"GetHidden", Expression_GetHidden, METH_VARARGS},
    {"SetType", Expression_SetType, METH_VARARGS},
    {"GetType", Expression_GetType, METH_VARARGS},
    {"SetFromDB", Expression_SetFromDB, METH_VARARGS},
    {"GetFromDB", Expression_GetFromDB, METH_VARARGS},
    {"SetFromOperator", Expression_SetFromOperator, METH_VARARGS},
    {"GetFromOperator", Expression_GetFromOperator, METH_VARARGS},
    {"SetOperatorName", Expression_SetOperatorName, METH_VARARGS},
    {"GetOperatorName", Expression_GetOperatorName, METH_VARARGS},
    {"SetMeshName", Expression_SetMeshName, METH_VARARGS},
    {"GetMeshName", Expression_GetMeshName, METH_VARARGS},
    {"SetDbName", Expression_SetDbName, METH_VARARGS},
    {"GetDbName", Expression_GetDbName, METH_VARARGS},
    {"SetAutoExpression", Expression_SetAutoExpression, METH_VARARGS},
    {"GetAutoExpression", Expression_GetAutoExpression, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
Expression_dealloc(PyObject *v)
{
   ExpressionObject *obj = (ExpressionObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *Expression_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyExpression_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "name") == 0)
        return Expression_GetName(self, NULL);
    if(strcmp(name, "definition") == 0)
        return Expression_GetDefinition(self, NULL);
    if(strcmp(name, "hidden") == 0)
        return Expression_GetHidden(self, NULL);
    if(strcmp(name, "type") == 0)
        return Expression_GetType(self, NULL);
    if(strcmp(name, "Unknown") == 0)
        return PyInt_FromLong(long(Expression::Unknown));
    if(strcmp(name, "ScalarMeshVar") == 0)
        return PyInt_FromLong(long(Expression::ScalarMeshVar));
    if(strcmp(name, "VectorMeshVar") == 0)
        return PyInt_FromLong(long(Expression::VectorMeshVar));
    if(strcmp(name, "TensorMeshVar") == 0)
        return PyInt_FromLong(long(Expression::TensorMeshVar));
    if(strcmp(name, "SymmetricTensorMeshVar") == 0)
        return PyInt_FromLong(long(Expression::SymmetricTensorMeshVar));
    if(strcmp(name, "ArrayMeshVar") == 0)
        return PyInt_FromLong(long(Expression::ArrayMeshVar));
    if(strcmp(name, "CurveMeshVar") == 0)
        return PyInt_FromLong(long(Expression::CurveMeshVar));
    if(strcmp(name, "Mesh") == 0)
        return PyInt_FromLong(long(Expression::Mesh));
    if(strcmp(name, "Material") == 0)
        return PyInt_FromLong(long(Expression::Material));
    if(strcmp(name, "Species") == 0)
        return PyInt_FromLong(long(Expression::Species));

    if(strcmp(name, "fromDB") == 0)
        return Expression_GetFromDB(self, NULL);
    if(strcmp(name, "fromOperator") == 0)
        return Expression_GetFromOperator(self, NULL);
    if(strcmp(name, "operatorName") == 0)
        return Expression_GetOperatorName(self, NULL);
    if(strcmp(name, "meshName") == 0)
        return Expression_GetMeshName(self, NULL);
    if(strcmp(name, "dbName") == 0)
        return Expression_GetDbName(self, NULL);
    if(strcmp(name, "autoExpression") == 0)
        return Expression_GetAutoExpression(self, NULL);


    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyExpression_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyExpression_methods[i].ml_name),
                PyString_FromString(PyExpression_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyExpression_methods, self, name);
}

int
PyExpression_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "name") == 0)
        obj = Expression_SetName(self, args);
    else if(strcmp(name, "definition") == 0)
        obj = Expression_SetDefinition(self, args);
    else if(strcmp(name, "hidden") == 0)
        obj = Expression_SetHidden(self, args);
    else if(strcmp(name, "type") == 0)
        obj = Expression_SetType(self, args);
    else if(strcmp(name, "fromDB") == 0)
        obj = Expression_SetFromDB(self, args);
    else if(strcmp(name, "fromOperator") == 0)
        obj = Expression_SetFromOperator(self, args);
    else if(strcmp(name, "operatorName") == 0)
        obj = Expression_SetOperatorName(self, args);
    else if(strcmp(name, "meshName") == 0)
        obj = Expression_SetMeshName(self, args);
    else if(strcmp(name, "dbName") == 0)
        obj = Expression_SetDbName(self, args);
    else if(strcmp(name, "autoExpression") == 0)
        obj = Expression_SetAutoExpression(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
Expression_print(PyObject *v, FILE *fp, int flags)
{
    ExpressionObject *obj = (ExpressionObject *)v;
    fprintf(fp, "%s", PyExpression_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
Expression_str(PyObject *v)
{
    ExpressionObject *obj = (ExpressionObject *)v;
    return PyString_FromString(PyExpression_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *Expression_Purpose = "This class contains an expression.";
#else
static char *Expression_Purpose = "This class contains an expression.";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(ExpressionType,         \
                  "Expression",           \
                  ExpressionObject,       \
                  Expression_dealloc,     \
                  Expression_print,       \
                  PyExpression_getattr,   \
                  PyExpression_setattr,   \
                  Expression_str,         \
                  Expression_Purpose,     \
                  Expression_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
Expression_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &ExpressionType
         || Py_TYPE(other) != &ExpressionType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    Expression *a = ((ExpressionObject *)self)->data;
    Expression *b = ((ExpressionObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static Expression *defaultAtts = 0;
static Expression *currentAtts = 0;

static PyObject *
NewExpression(int useCurrent)
{
    ExpressionObject *newObject;
    newObject = PyObject_NEW(ExpressionObject, &ExpressionType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new Expression(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new Expression(*defaultAtts);
    else
        newObject->data = new Expression;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapExpression(const Expression *attr)
{
    ExpressionObject *newObject;
    newObject = PyObject_NEW(ExpressionObject, &ExpressionType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (Expression *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
Expression_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewExpression(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef ExpressionMethods[] = {
    {"Expression", Expression_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *ExpressionObserver = 0;

std::string
PyExpression_GetLogString()
{
    std::string s("Expression = Expression()\n");
    if(currentAtts != 0)
        s += PyExpression_ToString(currentAtts, "Expression.", true);
    return s;
}

static void
PyExpression_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("Expression = Expression()\n");
        s += PyExpression_ToString(currentAtts, "Expression.", true);
        cb(s);
    }
}

void
PyExpression_StartUp(Expression *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyExpression_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(ExpressionObserver == 0)
    {
        ExpressionObserver = new ObserverToCallback(subj,
            PyExpression_CallLogRoutine, (void *)data);
    }

}

void
PyExpression_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete ExpressionObserver;
    ExpressionObserver = 0;
}

PyMethodDef *
PyExpression_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return ExpressionMethods;
}

bool
PyExpression_Check(PyObject *obj)
{
    return (obj->ob_type == &ExpressionType);
}

Expression *
PyExpression_FromPyObject(PyObject *obj)
{
    ExpressionObject *obj2 = (ExpressionObject *)obj;
    return obj2->data;
}

PyObject *
PyExpression_New()
{
    return NewExpression(0);
}

PyObject *
PyExpression_Wrap(const Expression *attr)
{
    return WrapExpression(attr);
}

void
PyExpression_SetParent(PyObject *obj, PyObject *parent)
{
    ExpressionObject *obj2 = (ExpressionObject *)obj;
    obj2->parent = parent;
}

void
PyExpression_SetDefaults(const Expression *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new Expression(*atts);
}

